package cn.example.notificationtest;



import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {
    private Button btn_send_notice;
    private NotificationManager manager;
    private Notification notification;

    //
    private static final int TAKE_PHOTO = 1;
    private Button btn_take_photo;
    private ImageView iv_picture;

    private Uri imageUri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        btn_send_notice=findViewById(R.id.btn_send_notice);
        btn_send_notice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, NotificationActivity.class);
                PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
                manager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                notification=new NotificationCompat.Builder(MainActivity.this,"yes")
                        .setContentTitle("This is content title")
                        .setContentText("This is content text")
                        .setWhen(System.currentTimeMillis())
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setLargeIcon(BitmapFactory.decodeResource(getResources(),
                                R.mipmap.ic_launcher))
                        .setContentIntent(pendingIntent)
                        .setDefaults(NotificationCompat.DEFAULT_ALL)
                        .build();
                manager.notify(1,notification);
            }
        });


        btn_take_photo= (Button) findViewById(R.id.btn_take_photo);
        iv_picture= (ImageView) findViewById(R.id.iv_picture);
        btn_take_photo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //创建File文件，用于储存拍照后的照片
                //getExternalCacheDir()关联缓存目录（专门存放缓存的目录）
                //具体路径/sdcard/Android/data/包名/cache
                //Android6.0，SD卡被列为危险权限，放在其他目录就要进行权限处理
                File outputImage=new File(getExternalCacheDir(),"output_image.jpg");

                try {
                    if (outputImage.exists()){
                        outputImage.delete();
                    }
                    outputImage.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                //Android7.0 调用FileProvider.getUriForFile()封装成Uri的字符串，接收3个参数,第一个:上下文对象,第二个：任意唯一字符串，第三个，File对象
                //7.0以下调用Uri.fromFile()将File对象转化Uri
                //FileProvider是一种特殊的内容提供器，对数据进行保护，选择性的封装过的Uri共享给外部，提高应用安全性。
                if (Build.VERSION.SDK_INT>=24){
                    imageUri= FileProvider.getUriForFile(MainActivity.this,"com.example.hjw.cameraalbumtest",outputImage);
                }else{
                    imageUri=Uri.fromFile(outputImage);
                }

                //启动相机
                Intent intent=new Intent("android.media.action.IMAGE_CAPTURE");
                intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);
                startActivityForResult(intent,TAKE_PHOTO);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case TAKE_PHOTO:
                if (resultCode==RESULT_OK){
                    try {
                        Bitmap bitmap= BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
                        iv_picture.setImageBitmap(bitmap);
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                }
                break;
            default:
                break;
        }
    }
}
